#ifndef MAIN_H
#define MAIN_H

//#define DEBUG 1

#include <string>
#include <sstream>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <iostream>
#include <vector>
#include <set>
#include <fstream>
#include "structures.h"

#define LEFT			0
#define RIGHT			1
#define INTERSECTION	2

using namespace std;

//poradie x, y : (+1) if p1 > p2; (-1) if p1 < p2; (0) p1 = p2
int xyorder( Point* p1, Point* p2 )
{
    // x-coord
    if (p1->x > p2->x) return 1;
    if (p1->x < p2->x) return (-1);
    // y-coord
    if (p1->y > p2->y) return 1;
    if (p1->y < p2->y) return (-1);
    return 0;  // they are the same point
}
//poradie eventov e1, e2: podla bodov, +ak su body rovnake, podla typu eventu
int eventorder( Event* e1, Event* e2 )
{
	int xy = xyorder( e1->eV, e2->eV);
	switch (xy)
	{
	case 1: return 1;
	case -1: return -1;
	case 0:
		//zoradenie eventov podla typu eventu
		if (e1->type == LEFT) return -1;
		if (e2->type == LEFT) return 1;
		if (e1->type == INTERSECTION) return -1;
		if (e2->type == INTERSECTION) return 1;
		return 0;
	default:
		return 0;
	}
}
//poloha bodu p2 vzhladom k segmentu p0 p1, >0 ak je vlavo, 0 ak je na priamke, <0 ak je vpravo od segmentu
inline float isLeft( Point P0, Point P1, Point P2 )
{
    return (P1.x - P0.x)*(P2.y - P0.y) - (P2.x - P0.x)*(P1.y - P0.y);
}
//obsahuje sada vrchol s tymito suradnicami?
bool set_contains(set<Point*>* s, Point * p)
{
	set<Point*>::iterator it;

	for(it = s->begin(); it != s->end();)
	{
		if (((*it)->x == p->x)&&((*it)->y == p->y)) return true;
		it++;
	}
	return false;
}
//pre dve rovnobezne lajny, zisti ci bod p1 lezi na s1
bool liesOn(SLseg* s1, Point p1)
{
	//x coord ok
	if ((s1->lP.x <= p1.x) && (s1->rP.x >= p1.x))
		//y coord ok
		if ((s1->lP.y <= p1.y) && (s1->rP.y >= p1.y))
			return true;
	return false;
}
//zisti ci sa s1 a s2 ciastocne/uplne prekryvaju
bool overlap(SLseg* s1, SLseg* s2)
{
	if (liesOn(s1, s2->lP) || 
		liesOn(s1, s2->rP) ||
		liesOn(s2, s1->lP) ||
		liesOn(s2, s1->rP))
		return true;
	return false;
}
bool from_string(float& t, const string& s, ios_base& (*f)(ios_base&))
{
  istringstream iss(s);
  return (iss >> f >> t).fail();
}
#endif
 